<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<!DOCTYPE html>
<html>

	<head>
		<link rel="stylesheet" href="${ctx}/css/common.css" />
		<jsp:include page="../inc.jsp"></jsp:include>
		<meta http-equiv="X-UA-Compatible" content="edge" />
		<c:if test="${fn:contains(sessionInfo.resourceList, '/organization/edit')}">
			<script type="text/javascript">
				$.canEdit = true;
			</script>
		</c:if>
		<c:if test="${fn:contains(sessionInfo.resourceList, '/organization/delete')}">
			<script type="text/javascript">
				$.canDelete = true;
			</script>
		</c:if>
		<title>部门管理</title>
		<style type="text/css">
			.mynewBtn{
				text-align: center;
			}
		</style>
		<script type="text/javascript">
			var treeGrid;
			$(function() {
				treeGrid = $('#treeGrid').treegrid({
					url: '${ctx}/organization/treeGrid',
					idField: 'id',
					treeField: 'name',
					parentField: 'pid',
					fitColumns: false,
					border: false,
					frozenColumns: [
						[{
							title: 'id',
							field: 'id',
							width: 150,
							hidden: true
						}]
					],
					columns: [
						[{
							field: 'code',
							title: '编号',
							width: 30
						}, {
							field: 'name',
							title: '部门名称',
							align: 'left',
							width: 200
						}, {
							field: 'seq',
							title: '排序',
							width: 40
						}, {
							field: 'icon',
							title: '图标',
							width: 80
						}, {
							width: '150',
							title: '创建时间',
							field: 'createdatetime'
						}, {
							field: 'pid',
							title: '上级资源ID',
							width: 150,
							hidden: true
						}, {
							field: 'pname',
							title: '上级部门名称',
							width: 150
						}, {
							field: 'address',
							title: '地址',
							width: 150
						}, {
							field: 'domain',
							title: '域名',
							width: 150
						},{
							field: 'money',
							title: '账户余额',
							width: 100
						},{
							field: 'availableBalance',
							title: '可用余额',
							width: 100
						}, {
							field: 'action',
							title: '操作',
							width: 200,
							formatter: function(value, row, index) {
								var str = '';
								if($.canEdit) {
									str += $.formatString('<a href="javascript:void(0)" class="mynewBtn" onclick="editFun(\'{0}\');" >编辑</a>', row.id);
								}
								if($.canDelete) {
									str += $.formatString('<a href="javascript:void(0)" class="mynewBtn" onclick="deleteFun(\'{0}\');" >删除</a>', row.id);
								}
								return str;
							}
						}]
					],
					toolbar: '#toolbar'
				});
			});

			function editFun(id) {
				if(id != undefined) {
					treeGrid.treegrid('select', id);
				}
				var node = treeGrid.treegrid('getSelected');
				if(node) {
					parent.$.modalDialog({
						title: '编辑',
						width: 500,
						height: 400,
						href: '${ctx}/organization/editPage?id=' + node.id,
						buttons: [{
							text: '编辑',
							handler: function() {
								parent.$.modalDialog.openner_treeGrid = treeGrid; //因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
								var f = parent.$.modalDialog.handler.find('#organizationEditForm');
								f.submit();
							}
						}]
					});
				}
			}

			function deleteFun(id) {
				if(id != undefined) {
					treeGrid.treegrid('select', id);
				}
				var node = treeGrid.treegrid('getSelected');
				if(node) {
					parent.$.messager.confirm('询问', '您是否要删除当前资源？删除当前资源会连同子资源一起删除!', function(b) {
						if(b) {
							progressLoad();
							$.post('${ctx}/organization/delete', {
								id: node.id
							}, function(result) {
								if(result.success) {
									parent.$.messager.alert('提示', result.msg, 'info');
									treeGrid.treegrid('reload');
								} else {
									parent.$.messager.alert('提示', result.msg, 'info');
								}
								progressClose();
							}, 'JSON');
						}
					});
				}
			}

			function addFun() {
				parent.$.modalDialog({
					title: '添加',
					width: 500,
					height: 400,
					href: '${ctx}/organization/addPage',
					buttons: [{
						text: '添加',
						handler: function() {
							parent.$.modalDialog.openner_treeGrid = treeGrid; //因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
							var f = parent.$.modalDialog.handler.find('#organizationAddForm');
							f.submit();
						}
					}]
				});
			}
		</script>
	</head>

	<body>
	<jsp:include page="../head.jsp"></jsp:include>
		<div class="divBox">
			<div>
				<table id="treeGrid" class="easyui-datagrid"></table>
			</div>

			<div id="toolbar" style="display: none;">
				<c:if test="${fn:contains(sessionInfo.resourceList, '/organization/add')}">
					<a onclick="addFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon_add'">添加</a>
				</c:if>
			</div>
		</div>
	<jsp:include page="../footer.jsp"/>
	</body>

</html>